import random

def sortArray(array):
    for i in range(0,len(array)):
        for j in range(i+1,len(array)):
            if array[i]>array[j]:
                tmp=array[i]
                array[i]=array[j]
                array[j]=tmp
    return array

def binSearch(array,bean):
    if len(array)==0:
        return -1
    mid=len(array)>>1
    if array[mid]==bean:
        return mid
    elif array[mid]<bean:
        newResult=binSearch(array[mid+1:],bean)
        if newResult>0:
            return mid+newResult+1
        else:
            return -1
    else:
        return binSearch(array[:mid],bean)

def binSearchMethod2(array,bean):
  
    low=0
    hight=len(array)
    while hight>low:
        mid=(hight+low)>>1
        if array[mid]==bean:
            return mid
        elif array[mid]<bean:
            low=mid+1
        else:
            hight=mid
    return -1

def main():
    testArray=[]
    while len(testArray)<20:
        testArray.append(random.randint(0,100))
    sortArray(testArray)
    print(testArray)
    bean=15
    print(bean)
    print(binSearch(testArray,bean))
    print("方法二===",binSearchMethod2(testArray,bean))

if __name__=='__main__':
    main()
